const HOST = '192.168.31.221:10086';
// const HOST = '10.0.1.85:10086';

const baseURL = `http://${HOST}/sv`

const wsURL = `ws://${HOST}/ws`

const interceptors = [];

const request = function(method, url, data,headers) {
	const options = {
		method: method || "GET",
		url:baseURL+url,
		header:{}
	}

	if (headers) {
		options.header = headers;
	}
	if(data){
		options.data = data;
	}
	
	for(let i = 0;i<interceptors.length;i++){
		interceptors[i](options);
	}
	
	return new Promise((resolve,reject)=>{
		uni.request({
			...options,
			success(data) {
				resolve(data);
			},
			fail(err){
				reject(err)
			}
		})
	})
	
}

request.post = function(url,data,headers){

	return request("POST",url,data,headers);
}

request.get = function(url,headers){
	return request("GET",url,null,headers);
}

request.use = function(useHandler){
	interceptors.push(useHandler);
}

//*************************************************************

import {LoginUtil} from '@/utils/index.js'

request.use(config=>{
	console.log("config",config)
	const token = LoginUtil.getToken();
	if(token){
		config.header['token']=token;
	}
})

const api = {
	getWsUrl(){
		return wsURL;
	},
	user:{
		login(entity){
			return request.post('/login',entity);
		},
		sendEmail(email){
			return request.post('/sendEmail',{email:email});
		},
		getUserInfo(){
			return request.get('/getUserInfo');
		},
		setUserName(name){
			return request.post('/setUserInfo',{name:name});
		}
	},
	qrcode:{
		scanCode(channelId){
			return request.post('/scanCode',{channelId:channelId});
		},
		confim(entity){
			return request.post('/confim',entity);
		}
	}


}



export default api;
